home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / reposition_windows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  5.1 KB  |  175 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include "mac-specific.h"
  3. #include "TextDisplay.h"
  4.  
  5. extern WindowPtr    g_page_window,
  6.                     g_freemem_window,
  7.                     g_console_window,
  8.                     g_cancel_dialog;
  9. extern Rect            g_screenrect;
  10.  
  11. typedef struct WPos {  /* custom resource 'WPos' to record window positions */
  12.     Rect preferred;
  13.     Rect fallback;
  14. } Wpos, *PWPos, **HWPos;
  15.  
  16. /* routines defined externally */
  17.  
  18. /* routines used externally */
  19. void    Reposition_windows( void );
  20. void    Save_window_positions( void );
  21. void    Center_window(    WindowPtr my_window );
  22.  
  23. /* private routines */
  24. Boolean    Window_on_screen( Rect *content_rect, Boolean title_bar );
  25. void    Reposition_window( WindowPtr the_window, HWPos pos, Boolean title_bar );
  26. void    Set_window_rect( WindowPtr the_window, Rect *the_rect );
  27. Point    Get_window_location( WindowPtr the_window );
  28. void    Update_WPos( char *res_name, WindowPtr window );
  29.  
  30. /* ----------------------- Save_window_positions ----------------------- */
  31. void
  32. Save_window_positions()
  33. {
  34.     Update_WPos( "page", g_page_window );
  35.     Update_WPos( "console", g_console_window );
  36.     Update_WPos( "FreeMem", g_freemem_window );
  37.     Update_WPos( "cancel", g_cancel_dialog );
  38.     UpdateResFile( g_pref_resfile );
  39.     (void) FlushVol( NIL, g_pref_vRefNum );
  40. }
  41.  
  42. /* ---------------------- Update_WPos ---------------------------- */
  43. void Update_WPos( char *res_name, WindowPtr window )
  44. {
  45.     short    res_id;
  46.     HWPos    res_h;
  47.     Rect    actual_rect;
  48.     
  49.     Get_resource( 'WPos', res_name, (Handle *)&res_h, &res_id );
  50.     /*
  51.         Previously I was using the bounding box of the content region
  52.         as the window's rectangle.  But that doesn't work if the
  53.         window is invisible.
  54.     */
  55.     actual_rect = window->portRect;
  56.     SetPort( window );
  57.     LocalToGlobal( &topLeft(actual_rect) );
  58.     LocalToGlobal( &botRight(actual_rect) );
  59.     if (!EqualRect( &actual_rect, &(**res_h).preferred ))
  60.     {
  61.         (**res_h).preferred = actual_rect;
  62.         ChangedResource( (Handle) res_h );
  63.     }
  64. }
  65.  
  66. /* ---------------------- Reposition_windows ---------------------------- */
  67. void
  68. Reposition_windows()
  69. {
  70.     HWPos pos;
  71.     short res_id;
  72.     
  73.     Get_resource( 'WPos', "page", (Handle *)&pos, &res_id );
  74.     Reposition_window( g_page_window, pos, FALSE );
  75.     Get_resource( 'WPos', "console", (Handle *)&pos, &res_id );
  76.     Reposition_window( g_console_window, pos, TRUE );
  77.     SetPort( g_console_window );
  78.     TD_resize();
  79.     ShowWindow( g_console_window );
  80.     Get_resource( 'WPos', "FreeMem", (Handle *)&pos, &res_id );
  81.     Reposition_window( g_freemem_window, pos, FALSE );
  82.     Get_resource( 'WPos', "cancel", (Handle *)&pos, &res_id );
  83.     Reposition_window( g_cancel_dialog, pos, TRUE );
  84. }
  85.  
  86. /* ---------------------- Reposition_window ---------------------------- */
  87. void
  88. Reposition_window( WindowPtr the_window, HWPos pos, Boolean title_bar )
  89. {
  90.     HLock( (Handle) pos );
  91.     if ( Window_on_screen( &(**pos).preferred, title_bar ) )
  92.         Set_window_rect( the_window, &(**pos).preferred );
  93.     else
  94.         Set_window_rect( the_window, &(**pos).fallback );
  95.     HUnlock( (Handle) pos );
  96. }
  97.  
  98. /* ---------------------- Set_window_rect ----------------------------- */
  99. void
  100. Set_window_rect( WindowPtr the_window, Rect *the_rect )
  101. {
  102.     MoveWindow( the_window, the_rect->left, the_rect->top, FALSE );
  103.     if (!EqualPt( Get_window_location(the_window), topLeft(*the_rect)))
  104.     {
  105.         printf("\nThat's funny, a MoveWindow failed. I'll try again.\n");
  106.         MoveWindow( the_window, the_rect->left, the_rect->top, FALSE );
  107.     }
  108.     SizeWindow( the_window, the_rect->right - the_rect->left,
  109.         the_rect->bottom - the_rect->top, FALSE );
  110. }
  111.  
  112. /* --------------------- Window_on_screen ------------------------------- */
  113. Boolean
  114. Window_on_screen( Rect *content_rect, Boolean title_bar )
  115. {
  116.     Rect    structure_rect;
  117.     RgnHandle    structure_rgn, intersect_rgn, gray_rgn;
  118.     Boolean        retval;
  119.     
  120.     structure_rect = *content_rect;
  121.     if (title_bar)
  122.         structure_rect.top -= 20;
  123.     structure_rgn = NewRgn();
  124.     RectRgn( structure_rgn, &structure_rect );
  125.     gray_rgn = GetGrayRgn();
  126.     if (EmptyRgn(gray_rgn))
  127.         printf("\nThat's weird, the gray region is empty.\n");
  128.     intersect_rgn = NewRgn();
  129.     SectRgn( gray_rgn, structure_rgn, intersect_rgn );
  130.     retval = EqualRgn( structure_rgn, intersect_rgn ) &&
  131.         !EmptyRgn(structure_rgn);
  132.     DisposeRgn( intersect_rgn );
  133.     DisposeRgn( structure_rgn );
  134.     return( retval );
  135. }
  136.  
  137. /* ---------------------- Get_window_location ------------------------- */
  138. /* 
  139.     Find a window's top left corner.  Sort of an inverse to MoveWindow.
  140. */
  141. Point
  142. Get_window_location( WindowPtr the_window )
  143. {
  144.     Point corner;
  145.     GrafPtr    save_port;
  146.     
  147.     GetPort( &save_port );
  148.     SetPort( the_window );
  149.     corner = topLeft( the_window->portRect );
  150.     LocalToGlobal( &corner );
  151.     SetPort( save_port );
  152.     return( corner );
  153. }
  154.  
  155. /* ----------------------- Center_window ----------------------- */
  156. void
  157. Center_window( the_window )
  158. WindowPtr the_window;
  159. {
  160.     Rect oldrect;
  161.     Point corner;
  162.     
  163.     oldrect = the_window->portRect; /* local coords */
  164.     corner.h = g_screenrect.left +
  165.         (g_screenrect.right - g_screenrect.left - oldrect.right) / 2;
  166.     corner.v = g_screenrect.top +
  167.         (g_screenrect.bottom - g_screenrect.top - oldrect.bottom) / 3;
  168.     MoveWindow( the_window, corner.h, corner.v, FALSE );
  169.     if (!EqualPt( Get_window_location(the_window), corner ))
  170.     {
  171.         printf("That's funny, a MoveWindow failed.  I'll try again.\n");
  172.         MoveWindow( the_window, corner.h, corner.v, FALSE );
  173.     }
  174. }
  175.